/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.db.connection.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import edu.uwm.universitydb.db.connection.DBConnection;
import edu.uwm.universitydb.framework.exception.DataAccessErrorCode;
import edu.uwm.universitydb.framework.exception.DataAccessException;

/**
 * <code>MySQLDBConnection</type> 
 * 
 * My SQL based implementation of DB Connection
 * 
 * @author <Enter your name here>
 */
public class MySQLDBConnection implements DBConnection
{

    /*
     * (non-javadoc)
     * @see edu.uwm.cs757.db.connection.DBConnection#getConntection(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public Connection getConntection(String url, String username, String password) throws DataAccessException
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url,username, password);
            return connection;
        }
        catch (ClassNotFoundException e)
        {
            final String eMessage = e.getMessage();
            System.out.println(" Error Obtaining Connection " + eMessage);
            throw new DataAccessException(DataAccessErrorCode.DRIVER_NOT_FOUND, eMessage, e);
        }
        catch (SQLException e)
        {
            final String eMessage = e.getMessage();
            System.out.println(" Error Obtaining Connection " + eMessage);
            final int errorCode =  e.getErrorCode();
            if(errorCode == 123) //TODO find the actual code
            {
                throw new DataAccessException(DataAccessErrorCode.INAVLID_LOGIN, eMessage, e);
            }
            else
            {
                throw new DataAccessException(DataAccessErrorCode.UNCATEGAROZIED_ERROR, eMessage, e);
            }
        }
    }

}
/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 6, 2014   <TODO Enter your name here>
 * File created
 * ----------------------------------------------------------------------------
 */